package com.smcaiot.filesync.starter.handler.impl;

import cn.hutool.core.util.StrUtil;
import com.smcaiot.filesync.starter.entity.Base64Param;
import com.smcaiot.filesync.starter.handler.FileHandler;
import com.smcaiot.filesync.starter.service.IMyMinioService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author: cz
 * @Date: 2023/6/28
 * @Description: 默认文件服务器交互类，使用 minio 实现
 */
@Slf4j
public class DefaultFileHandler implements FileHandler {

    @Autowired(required = false)
    private IMyMinioService minioService;

    @Override
    public void download(String remotePath, String localPath) {
        minioService.getMinioFile(remotePath, localPath);
    }

    @Override
    public String downloadBase64(String filePath) {
        String base64Str = minioService.getFileBase64(filePath);
        log.info("下载文件base64：{}", StrUtil.isNotBlank(base64Str));
        return base64Str;
    }

    @Override
    public String upload(String localPath, String destPath, boolean nameWithTime) {
        return minioService.uploadMinioFile(localPath, destPath, nameWithTime);
    }

    @Override
    public String uploadBase64(Base64Param base64Param) {
        try {
            return minioService.base64StoreFile(base64Param);
        } catch (Exception e) {
            log.warn("文件base64保存失败：{}", e);
            return null;
        }
    }

}
